// 小程序开发api接口统一配置

import store from './../utils/store'

// 如果你的域名是： https://www.baidu.com/cn 那么这里只要填写 cn
let subDomain = ''  // 子域名,没有就等于''
// const API_BASE_URL = 'http://192.168.68.118:12011'  // 主域名 
// const API_BASE_URL = 'http://boboan.natapp1.cc'  // 主域名 
const API_BASE_URL = 'https://www.chaoshenkeji.cn:12011'

const request = (url, method, data, contentType, token) => {
  let _url = API_BASE_URL + subDomain  + url
  let header = token ? {
        'Content-Type': contentType ? contentType:'application/json'
      }:{
        'Content-Type': contentType ? contentType:'application/json',
        'Authorization': 'Bearer ' + store.getItem('userInfo').access_token
      }
  return new Promise((resolve, reject) => {
    // 上传文件
    if(contentType == 'multipart/form-data'){
      wx.uploadFile({
        url: _url,
        filePath: data.file,
        name: 'file',
        header,
        success(request) {
          console.log(request)
          resolve(request.data)
        },
        fail(error) {
          reject(error)
        },
        complete(aaa) {
          // 加载完成
        }
      })
      return;
    }
    // 请求接口
    wx.request({
      url: _url,
      method: method,
      data: data,
      header,
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}
 
/**
 * 小程序的promise没有finally方法，自己扩展下
*/
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}
 
module.exports = {
  request,
  // 登录
  login: data => request('/business_user/login','post', data, false, true),
  // 文件上传接口
  upload: data => request('/qiniu/upload', 'post', data, 'multipart/form-data'),

  // 店铺注册接口
  registerShop: (data) => request('/business_shop/saveOrUpdate','post', data),
  // 店铺列表
  getShopList: (data) => request('/business_shop/list','post', data, 'application/x-www-form-urlencoded'),
  // 店铺详情
  shopDetail: (data) => request('/business_shop/detail','post', data, 'application/x-www-form-urlencoded'),
  // 开关店
  setShopOpenStatus: (data) => request('/business_shop/setShopOpenStatus','post', data,'application/x-www-form-urlencoded'),
  
  // 添加或修改商品
  addGoods: (data) => request('/business_spu/saveOrUpdate', 'post', data),
//上架下架置顶商品
  upOrDown: (data) => request('/business_spu/upOrDown', 'post', data,'application/x-www-form-urlencoded'),
  // 商品列表
  goodsList: (data) => request('/business_spu/findAll','get', data, 'application/x-www-form-urlencoded'),
  // 商品分类列表
  goodsTypeList: (data) => request('/business_category/findAll', 'get', data, 'application/x-www-form-urlencoded'),
  // 新增分类
  addGoodsType: (data) => request('/business_category/saveOrUpdate', 'post', data),
  // 商品规格列表
  getSpecifications: (data) => request('/business_spec/findAll', 'get', data, 'application/x-www-form-urlencoded'),
  // 新增规格
  addSpecs: (data) => request('/business_spec/saveOrUpdate', 'post', data),
  // 删除商品
  deleteGoods: (data) => request('/business_spu/delete', 'DELETE', data,'application/x-www-form-urlencoded'),
  // 批量删除商品
  deleteBatch: (data) => request('/business_spu/deleteBatch', 'DELETE', data,'application/x-www-form-urlencoded'),

  // 订单列表
  orderList: (data) => request('/business_order/findAllOrder', 'get', data,'application/x-www-form-urlencoded'),
  // 订单明细列表
  orderDetailList: (data) => request('/business_order/findAllOrderDetail', 'get', data,'application/x-www-form-urlencoded'),
  // 订单详情
  orderDetail: (data) => request('/business_order/findOrderDetail', 'get', data,'application/x-www-form-urlencoded'),
  // 修改订单状态
  setOrderStatus: (data) => request('/business_order/setOrderStatus', 'get', data),
  

  // 活动列表
  activeList: (data) => request('/business_activity/list', 'post', data,'application/x-www-form-urlencoded'),
  // 活动详情
  activeDetail: (data) => request('/business_activity/detail', 'post', data,'application/x-www-form-urlencoded'),
  // 新增活动
  addActive: (data) => request('/business_activity/saveOrUpdate', 'post', data),
  // 删除活动
  deleteActive: (data) => request('/business_activity/delete', 'DELETE', data,'application/x-www-form-urlencoded'),

//退出
  logout: (data) => request('/business_user/logout','post', data, 'application/x-www-form-urlencoded'),

  index: (data) => request('/business_index/statistics', 'post', data, 'application/x-www-form-urlencoded'),

//请求微信token
  getWxToken: (data) => request('/wx/access/token', 'get', data, 'application/x-www-form-urlencoded'),

//获取商户门店二维码
  getQrCode: (data) => request('/wx/access/getBase64','post', data, 'application/x-www-form-urlencoded'),

}